/*
* @Author: chenjianghui
* @Date:   2018-08-31 17:13:38
* @Last Modified by:   chenjianghui
* @Last Modified time: 2018-09-11 17:16:51
*/
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'manager/user/index',
                    add_url: 'manager/user/add',
                    edit_url: 'manager/user/edit',
                    del_url: 'manager/user/del',
                    resetpsw_url: 'manager/user/resetpsw_url',
                    multi_url: 'manager/user/multi',
                    ajax_url:{
                        btn_1:{data:{'row[field]':"status"},url:"manager/user/update_status",fc:function(data){
                                return true;
                            }},
                        // btn_2:{data:{'field':"role"},url:"microend/paike/update_status",fc:function(data){
                        //         return true;
                        //     }},
                        // btn_3:{data:{'field':"black"},url:"microend/paike/update_status",fc:function(data){
                        //         return true;
                        //     }},
                        // // btn_4:{data:{"type":1},url:"microend/paike/update_status",fc:function(data){
                        // //     return true;
                        // //     }}
                        // btn_4:'microend/paike/edit_user?type=1',
                        // btn_5:'microend/paike/edit_user?type=2',
                        // btn_6:'microend/paike/edit_user?type=3',
                        btn_7:'manager/user/resetpsw'
                    },
                    btn_mall_window:{
                        btn_array:["立即提交","取消"],
                        btn_evn_array_length:2,
                        btn_evn_array: {
                            evn_1:function (index,layero) {
                                var frame = Layer.getChildFrame('html', index);
                                var form = $("form",frame);
                                var success = function (data, ret) {
                                    var data = data.responseJSON;
                                    if(data.code == 1){
                                        layer.msg(data.msg);
                                        table.bootstrapTable('refresh');
                                        Layer.close(index);
                                    }else{
                                        layer.msg(data.msg)
                                    }
                                    console.log(data.responseJSON)
                                    //console.log(ret)

                                }
                                Fast.api.form_ajax_submit(form,success);
                            },
                            evn_2:function (index,layero) {
                                Layer.close(index);
                            }
                        },
                        area:['700px','400px']
                    },
                    table: 'user',
                }
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
                        $("input[type=checkbox]", this).prop("disabled", true);
                    }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID', operate: false},
                        {field: 'username', title: __('Username')},
                        {field: 'nickname', title: __('Nickname'), operate:false},
                        {field: 'g.name', title: __('Group'), formatter: Table.api.formatter.label},
                        {field: 'platform', title: __('Platform'), operate:false},
                        // {field: 'email', title: __('Email')},
                        {field: 'a.status', title: __("Status"),searchList: {"normal":__('正常'),"hidden":__('禁用')},formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                var index = keys.indexOf(value.toString());
                                value = value === null ? '' : value.toString();
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {field: 'a.createtime', title:'添加时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table,
                            // buttons: [
                            //     {name: 'name2', text: '设置密码', title: '设置密码', icon: 'fa fa-flash', classname: 'btn btn-xs btn-primary btn-dialog', url: 'manager/user/resetpsw', success:function(data, ret){}, error:function(){}},
                            // ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {

                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);

                                if (options.extend.dragsort_url !== '') {
                                    buttons.push({
                                        name: 'dragsort',
                                        icon: 'fa fa-arrows',
                                        title: __('Drag to sort'),
                                        extend: 'data-toggle="tooltip"',
                                        classname: 'btn btn-xs btn-primary btn-dragsort'
                                    });
                                }
                                if (options.extend.edit_url !== '') {
                                    buttons.push({
                                        name: 'edit',
                                        icon: 'fa fa-pencil',
                                        title: __('Edit'),
                                        extend: 'data-toggle="tooltip"',
                                        classname: 'btn btn-xs btn-success btn-editone',
                                        url: options.extend.edit_url
                                    });
                                }
                                buttons.push(
                                    {
                                        name: 'btn_1',
                                        text: row.status== '正常'?__('Jin_yong'):__('Qi_yong'),
                                        extend: 'data-toggle="tooltip" data-name="btn_1"',
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                    },
                                    {
                                        name: 'btn_7',
                                        text: "重置密码",
                                        extend: 'data-toggle="tooltip" data-name="btn_7" data-title="重置密码" ',
                                        classname: 'btn btn-xs btn-success btn-magic btn-small-window ',
                                    });
                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                            }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('input[name="row[platform]"]').on('click',function(){
                if($(this).val() == 1){
                    $("#bank").css('display','none')
                }else{
                    $("#bank").css('display','block')
                }
                console.log()
            })
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            $('input[name="row[platform]"]').on('click',function(){
                if($(this).val() == 1){
                    $("#bank").css('display','none')
                }else{
                    $("#bank").css('display','block')
                }
                console.log()
            })
            Form.api.bindevent($("form[role=form]"));
        },
        resetpsw: function () {
            Form.api.bindevent($("form[role=form]"));
        }
    };
    return Controller;
});